﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace Controllers
{
    public class AnnualController : BaseController
    {
        //分页查询
        public ActionResult Find(int start, int limit, string idStr, string name)
        {
            int empId;
            IList<int> deptIdList = getAuthedDeptIds("Annual", idStr, out empId);

            IQueryable<AnnualLeaves> q = from r in ctx.AnnualLeaves
                                         select r;
            if (empId != 0)
            {
                q = q.Where(r => r.empid == empId);
            }
            else
            {
                q = q.Where(r => (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.empid));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                q = q.Where(r => r.name.Contains(name));
            }
            int totalCnt = q.Count();
            IList<AnnualLeaves> list = q.OrderBy(r => r.empid).Skip(start).Take(limit).ToList();

            CJson CJson = new CJson();
            string json = CJson.ToJsonString("totalCount:" + totalCnt + ",data", list);
            return Content(json);
        }

        public int GetnianjiaByID(int empid)
        {
            AnnualLeaves model = ctx.AnnualLeaves.Single(q => q.empid == empid);
            return model.annualwork;
        }

        //导出excel
        public void GetExcel()
        {
            IQueryable<AnnualLeaves> list = ctx.AnnualLeaves;

            MemoryStream ms = new MemoryStream();

            using (Workbook workbook = new HSSFWorkbook())
            {
                using (Sheet sheet = workbook.CreateSheet())
                {
                    Row headerRow = sheet.CreateRow(0);

                    headerRow.CreateCell(0).SetCellValue("部门");
                    headerRow.CreateCell(1).SetCellValue("姓名");
                    headerRow.CreateCell(2).SetCellValue("首次工作日期");
                    headerRow.CreateCell(3).SetCellValue("入职日期");
                    headerRow.CreateCell(4).SetCellValue("总工龄(年)");
                    headerRow.CreateCell(5).SetCellValue("是否满一年");
                    headerRow.CreateCell(6).SetCellValue("年假数");
                    headerRow.CreateCell(7).SetCellValue("已休天数");

                    // handling value.
                    int rowIndex = 1;

                    foreach (AnnualLeaves model in list.ToList())
                    {
                        Row dataRow = sheet.CreateRow(rowIndex);
                        dataRow.CreateCell(0).SetCellValue(model.dep);
                        dataRow.CreateCell(1).SetCellValue(model.name);
                        dataRow.CreateCell(2).SetCellValue(model.fristwork == null ? "" : model.fristwork.ToString());
                        dataRow.CreateCell(3).SetCellValue(model.trialDate.ToString("yyyy-mm-dd"));
                        dataRow.CreateCell(4).SetCellValue(model.sumworkdays);
                        dataRow.CreateCell(5).SetCellValue(model.onemoreyear);
                        dataRow.CreateCell(6).SetCellValue(model.annualwork);
                        dataRow.CreateCell(7).SetCellValue(model.hadused == null ? 0 : int.Parse(model.hadused.ToString()));
                        rowIndex++;
                    }
                    workbook.Write(ms);

                    ms.Position = 0;
                }
            }
            long FileSize = ms.Length;
            byte[] Buffer = new byte[(int)FileSize];
            ms.Read(Buffer, 0, (int)FileSize);

            string filename = DateTime.Now.ToString("yyyy-mm-dd") + "年假.xls";

            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
            Response.BinaryWrite(Buffer);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }
    }
}